package com.adhere.system.service.impl;

import com.adhere.system.mapper.RbacUserMapper;
import com.adhere.system.service.IRbacUserService;
import com.adhere.system.utils.CodeUtils;
import com.adhere.system.pojo.RbacUser;
import com.adhere.system.pojo.vo.PageVo;
import com.adhere.system.pojo.vo.UserDetailVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 如果考虑使用login_name进行hash作为code字段值，则没必要设置code的唯一性，否则需要设置 服务实现类
 * </p>
 *
 * @author 
 * @since 2021-04-02
 */
@Service
public class RbacUserServiceImpl extends ServiceImpl<RbacUserMapper, RbacUser> implements IRbacUserService {

    @Autowired
    private RbacUserMapper userMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;


    @Override
    public PageVo<RbacUser> getUserListByPage(Integer currentPage, Integer pageSize, String loginName, String roleCode) {
        Page<RbacUser> userPage = null;
        Page<RbacUser> rbacUserPage = new Page<>(currentPage, pageSize);
        if (!StringUtils.hasText(roleCode)) {
            // 不使用角色信息作为查询条件
            QueryWrapper<RbacUser> queryWrapper = new QueryWrapper<>();
            if (StringUtils.hasText(loginName)) {
                queryWrapper.like("login_name", loginName);
            }
            queryWrapper.eq("is_deleted", false);
            queryWrapper.orderByDesc("create_time");
            userPage = userMapper.selectPage(rbacUserPage, queryWrapper);
        } else {
            // 使用角色信息作为查询条件
            userPage = userMapper.getUserListByPage(rbacUserPage, loginName, roleCode);
        }

        return new PageVo<RbacUser>().setTotal(userPage.getTotal())
                .setCurrentPage(currentPage)
                .setPageSize(pageSize)
                .setObjectList(userPage.getRecords());
    }

    @Override
    public void addUser(RbacUser user) {
        QueryWrapper<RbacUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("login_name", user.getLoginName());
        RbacUser rbacUser = userMapper.selectOne(queryWrapper);
        if (rbacUser != null) {
            throw new IllegalArgumentException("登录名重复");
        }

        // 对密码进行加密
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        int result = userMapper.insert(user);
        if (result == 1) {
            // 填充code
            user.setCode(CodeUtils.encode(user.getId()));
            userMapper.updateById(user);
        }
    }

    @Override
    public UserDetailVo getUserDetailByUserName(String loginName) {
        return userMapper.getUserDetailByUserName(loginName);
    }

    /**
     * 根据角色code 查询用户列表
     * @param roleCode
     * @return
     */
    @Override
    public List<String> getUserListByRoleCode(String roleCode) {
        return userMapper.getUserListByRoleCode(roleCode);
    }

    @Override
    public UserDetailVo getUserDetailByUserCode(String userCode) {
        return userMapper.getUserDetailByUserCode(userCode);
    }

    @Override
    public List<RbacUser> getUserWithAdmin() {
        return userMapper.getUserWithAdmin();
    }
}
